package com.zdy.worldtion.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Build;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

/**
 * Created by wxl on 2022/1/14 14:48
 * 业务相关工具类
 */
public class BusinessUtils {

    /**
     * 根据cycle获取string内容
     *
     * @param cycle 1上周；2本月；3上月；4本年；5上年； 6本周
     * @return string
     */
    public static String getDateCycle(int cycle) {
        String dateInfo = "本月数据";
        switch (cycle) {
            case 1:
                dateInfo = "上周数据";
                break;
            case 2:
                dateInfo = "本月数据";
                break;
            case 3:
                dateInfo = "上月数据";
                break;
            case 4:
                dateInfo = "本年数据";
                break;
            case 5:
                dateInfo = "上年数据";
                break;
            case 6:
                dateInfo = "本周数据";
                break;
            default:
                dateInfo = "本月数据";
                break;
        }
        return dateInfo;
    }

    /**
     * 获取上月周期范围
     *
     * @param currentMonth 当前月份
     * @param currentDay   当前日期
     * @return string
     */
    public static String getLastMonthDateLimit(int currentMonth, int currentDay) {
        String dateLimit = "";
        if (currentMonth == 1) {
            if (currentDay >= 15) {
                dateLimit = "(12/15-1/14)";
            } else {
                dateLimit = "(11/15-12/14)";
            }
        } else if (currentMonth == 2) {
            if (currentDay >= 15) {
                dateLimit = "(1/15-2/14)";
            } else {
                dateLimit = "(12/15-1/14)";
            }
        } else {
            if (currentDay >= 15) {
                dateLimit = "(" + (currentMonth - 1) + "/15-" + currentMonth + "/14)";
            } else {
                dateLimit = "(" + (currentMonth - 2) + "/15-" + (currentMonth - 1) + "/14)";
            }
        }
        return dateLimit;
    }

    /**
     * 获取本月周期范围
     *
     * @param currentMonth 当前月份
     * @param currentDay   当前日期
     * @return string
     */
    public static String getThisMonthDateLimit(int currentMonth, int currentDay) {
        String dateLimit = "";
        if (currentMonth == 12) {
            if (currentDay >= 15) {
                dateLimit = "(" + currentMonth + "/15-1/14)";
            } else {
                dateLimit = "(11/15-12/14)";
            }
        } else if (currentMonth == 1) {
            if (currentDay >= 15) {
                dateLimit = "(1/15-2/14)";
            } else {
                dateLimit = "(12/15-1/14)";
            }
        } else {
            if (currentDay >= 15) {
                dateLimit = "(" + currentMonth + "/15-" + (currentMonth + 1) + "/14)";
            } else {
                dateLimit = "(" + (currentMonth - 1) + "/15-" + currentMonth + "/14)";
            }
        }
        return dateLimit;
    }

    /**
     * 跳转到系统设置
     *
     * @param context 上下文
     */
    public static void goToSetting(Context context) {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 9) {
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else {
            // 其他
            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 跳转到系统设置
     *
     * @param context 上下文
     */
    public static void goToSettingPage(Context context) {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            // android 8.0引导
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
        } else if (Build.VERSION.SDK_INT >= 21) {
            // android 5.0-7.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
        } else {
            // 其他
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    /**
     * 检查是否有某一权限
     *
     * @param context    上下文
     * @param permission 某一权限
     * @return 布尔值
     */
    public static boolean checkPermissions(Context context, String permission) {
        boolean isPermissions = false;//是否已申请了该权限，true已有该权限；false没有该权限
        int checkPermission = ContextCompat.checkSelfPermission(context, permission);
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                isPermissions = false;
            } else {
                isPermissions = true;
            }
        } else {
            isPermissions = false;
        }
        return isPermissions;
    }

    /**
     * 检查是否已开启消息通知
     *
     * @param context 上下文
     * @return 是否开启了消息通知，true开启，false不开启
     */
    public static boolean isOpenMsgNotice(Context context) {
        boolean isOpenMsgNotice = false;//是否开启了消息通知，true开启，false不开启
        try {
            isOpenMsgNotice = NotificationManagerCompat.from(context).areNotificationsEnabled();
        } catch (Exception e) {
            e.printStackTrace();
            isOpenMsgNotice = false;
        }
        return isOpenMsgNotice;
    }

    /**
     * 获取assets中的数据
     *
     * @param context 上下文
     * @return str
     */
    public static String getAssetsJson(Context context) {
        String jsonStr = "";
        try {
            AssetManager assetManager = context.getAssets(); // 获得assets资源管理器(assets中的文件无法直接访问，可以使用AssetManager访问)
            InputStreamReader inputStreamReader = new InputStreamReader(assetManager.open("city.json"), "UTF-8"); // 使用IO流读取json文件内容
            BufferedReader br = new BufferedReader(inputStreamReader);
            String line;
            StringBuilder builder = new StringBuilder();
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
            br.close();
            inputStreamReader.close();
            jsonStr = builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
            jsonStr = "";
        }
        return jsonStr;
    }

    /**
     * 获取版本名称
     *
     * @param context 上下文
     * @return str
     */
    public static String getVersionName(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
            return info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 字符串判断空
     * @param cs cs
     * @return bool
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
}
